package com.pristine.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pristine.dao.IMedicineDao;
import com.pristine.domain.MedicineEntity;
import com.pristine.service.wsdl.IMedicineService;
import com.pristine.service.wsdl.MedicineVO;
import com.pristine.util.EntityToVOConverter;
import com.pristine.util.VOToEntityConverter;

@Service("medicineMasterService")
@Transactional
public class MedicineServiceImpl implements IMedicineService {

	@Autowired
	private IMedicineDao medicineMasterDao;

	@Override
	public Integer createMedicineMaster(MedicineVO masterVO) {
		MedicineEntity medicineMaster = VOToEntityConverter
				.convert(masterVO);
		medicineMasterDao.add(medicineMaster);
		return medicineMaster.getId();
	}

	@Override
	public Integer updateMedicineMaster(MedicineVO masterVO) {
		MedicineEntity medicineMaster = VOToEntityConverter
				.convert(masterVO);
		medicineMasterDao.update(medicineMaster);
		return medicineMaster.getId();
	}

	@Override
	public boolean deleteMedicineMaster(Integer id) {
		try {
			MedicineEntity employee = medicineMasterDao.find(id);
			medicineMasterDao.remove(employee);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public MedicineVO getMedicineMasterById(Integer id) {
		MedicineEntity medicineMaster = medicineMasterDao.find(id);
		MedicineVO masterVO = EntityToVOConverter.convert(medicineMaster);
		return masterVO;
	}

	@Override
	public List<MedicineVO> getAllMedicineMaster() {

		List<MedicineEntity> list = medicineMasterDao.list();
		List<MedicineVO> voList= new ArrayList<MedicineVO>();
		if(CollectionUtils.isNotEmpty(list)){
			for(MedicineEntity entity:list){
				MedicineVO vo = EntityToVOConverter.convert(entity);
				voList.add(vo);
			}
		}
		return voList;
	
	}

}
